var Lib = {}

Lib.install = function (Vue, options) {

    Vue.prototype.$post = function (api, _data, cb, error_cb) {
        if (_data == null) {
            _data = {}
        }
        let token = this.$cookies.get("user_token")
        token=token==null?"":token
        _data.token = token
        let that = this
        this.axios.defaults.baseURL = 'https://s.beiyii.xyz/';
        this.axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
        let data = this.qs.stringify(_data)
        this.axios.post('https://s.beiyii.xyz/' + api, data, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        }).then(function (response) {

            if (response.status == 200) {
                let data = response.data
                if (data.error_code == 0) {
                    try{
                        cb(data.result)
                    }catch (e) {
                        console.error(e)
                    }

                } else if (data.error_code == 300) {
                    that.tologin()
                } else {
                    if (error_cb == null) {
                        that.$toast(data.message)
                    } else {
                        error_cb(data.message, data.error_code)
                    }
                }
            }else {
                that.$toast("其他异常")
            }

        }) .catch(function (error) {
            that.$toast("服务器异常")
        });
    }
    Vue.prototype.datainit = function (data) {
        data.resurl = "https://res.xckj789.com/"
        return data
    }
    Vue.prototype.check = function () {
        let that=this
        let group_code= this.$route.query.group_code
        this.$post("api/user/get", {group_code:group_code,url:window.location.href.toString()}, function (_user) {
            try{that.user_init(_user)}catch(e){

            }

        })
    }
    Vue.prototype.tologin = function () {
        let up_id=this.$cookies.get("up_id")
        let curl=window.location.href.toString()
        if(up_id==null){
            up_id=0;
        }
        let url = encodeURIComponent('https://s.beiyii.xyz/api/user/waplogin'+'?from=' + encodeURIComponent(curl));
        window.location.href='http://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe02758872ad8bc27&redirect_uri=' + url + '&response_type=code&scope=snsapi_userinfo&state=a2eba95667508f886f602a3ed3470394&connect_redirect=1#wechat_redirect'
    }
}

// 向外暴露接口
module.exports = Lib;